package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Transactional //事务
@Service
public class  TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    //自动注入mapper
    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;
    @Autowired
    private TbClueMapper tbClueMapper;
    /**m
     * 线索跟进记录
     * @param TbClueTrackRecordvo 页面传过来的数据
     */
    @Override
    public void addClueTrackRecordVo(ClueTrackRecordVo TbClueTrackRecordvo) {
        //1. 转换线索跟进的数据, 页面数据---数据库数据
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        //copy数据,tbClueTrackRecord是数据库数据,   TbClueTrackRecordvo是页面数据
        BeanUtils.copyBeanProp(tbClueTrackRecord,TbClueTrackRecordvo);
        //传入用户名,从数据库中传
        tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());
        //传入创建时间为当前时间
        tbClueTrackRecord.setCreateTime(new Date());
        //下次跟进时间
        tbClueTrackRecord.setNextTime(new Date());
      //添加数据到mapper
        tbClueTrackRecordMapper.add(tbClueTrackRecord);

        //2. 修改线索的状态
        TbClue tbClue = new TbClue();
        //copy数据,tbClueTrackRecord是数据库数据,   TbClueTrackRecordvo是页面数据
        BeanUtils.copyBeanProp(tbClue,TbClueTrackRecordvo);
        //获取此线索的id
        tbClue.setId(tbClueTrackRecord.getId());
        //获取此线索的状态
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbClueMapper.updateTbClue(tbClue);
    }

    @Override
    public List<TbClueTrackRecord> findByid(Long clueId) {
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.findByid(clueId);
        return list;
    }
}
